// -------- WIFI -------- #include // -------- FIREBASE -------- #include #include "addons/TokenHelper.h" #include "addons/RTDBHelper.h" // -------- WIFI CREDENTIALS -------- const char* ssid = "MiCho"; const char* password = "Hello1234"; // -------- FIREBASE CONFIG -------- #define API_KEY "AIzaSyDpefz07KOuYyj0mZ1YtvJkm5g3z5HU0-I" #define DATABASE_URL "https://smart-piggy-effc6-default-rtdb.asia-southeast1.firebasedatabase.app/" FirebaseData fbdo; FirebaseAuth auth; FirebaseConfig config; // -------- PIN SETUP -------- #define BUTTON_PIN D8 #define LED_PIN D2 // -------- TIMING CONFIG -------- const unsigned long debounceDelay = 50; const unsigned long longPressTime = 1000; const unsigned long doublePressGap = 400; // -------- SYSTEM STATE -------- int totalAmount = 0; int goal = 100; bool milestone25 = false; bool milestone50 = false; bool milestone75 = false; // -------- BUTTON STATE -------- bool buttonState = LOW; bool lastButtonReading = LOW; unsigned long lastDebounceTime = 0; unsigned long pressStartTime = 0; unsigned long lastReleaseTime = 0; int pressCount = 0; // -------- SETUP -------- void setup() { Serial.begin(115200); pinMode(BUTTON_PIN, INPUT_PULLDOWN); pinMode(LED_PIN, OUTPUT); digitalWrite(LED_PIN, LOW); // -------- WIFI CONNECT -------- Serial.println("Connecting to WiFi..."); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println("\nWiFi Connected!"); Serial.print("IP Address: "); Serial.println(WiFi.localIP()); // -------- FIREBASE SETUP -------- config.api_key = API_KEY; config.database_url = DATABASE_URL; // required for token handling config.token_status_callback = tokenStatusCallback; // anonymous authentication Firebase.signUp(&config, &auth, "", ""); Firebase.begin(&config, &auth); Firebase.reconnectWiFi(true); Serial.println("Firebase ready"); } // -------- LOOP -------- void loop() { int reading = digitalRead(BUTTON_PIN); // -------- DEBOUNCE -------- if (reading != lastButtonReading) { lastDebounceTime = millis(); } if ((millis() - lastDebounceTime) > debounceDelay) { if (reading != buttonState) { buttonState = reading; // button pressed if (buttonState == HIGH) { pressStartTime = millis(); } // button released else { unsigned long pressDuration = millis() - pressStartTime; // long press → ₹5 if (pressDuration >= longPressTime) { handleLongPress(); pressCount = 0; } else { pressCount++; lastReleaseTime = millis(); } } } } // detect single vs double press after gap if (pressCount > 0 && (millis() - lastReleaseTime) > doublePressGap) { if (pressCount == 1) { handleSinglePress(); } else if (pressCount == 2) { handleDoublePress(); } pressCount = 0; } lastButtonReading = reading; } // -------- ACTIONS -------- // ₹1 void handleSinglePress() { addAmount(1); blinkLED(1); } // ₹2 void handleDoublePress() { addAmount(2); blinkLED(2); } // ₹5 void handleLongPress() { addAmount(5); digitalWrite(LED_PIN, HIGH); delay(400); digitalWrite(LED_PIN, LOW); } // -------- FIREBASE SEND -------- // called only when value changes (inside addAmount) void sendToFirebase(int total, int percentage, int lastCoin) { if (Firebase.ready()) { Firebase.RTDB.setInt(&fbdo, "/piggy/total", total); Firebase.RTDB.setInt(&fbdo, "/piggy/percentage", percentage); Firebase.RTDB.setInt(&fbdo, "/piggy/lastCoin", lastCoin); Serial.println("Data sent to Firebase"); } } // -------- CORE LOGIC -------- void addAmount(int value) { totalAmount += value; int percentage = (totalAmount * 100) / goal; Serial.print("Added: "); Serial.print(value); Serial.print(" | Total: "); Serial.print(totalAmount); Serial.print(" | "); Serial.print(percentage); Serial.println("%"); // 🔥 Firebase update happens ONLY here sendToFirebase(totalAmount, percentage, value); // -------- GOAL CHECK WITH REMAINDER -------- if (totalAmount >= goal) { int overflow = totalAmount - goal; Serial.println("Milestone: Goal reached!"); // celebration blink for (int i = 0; i < 3; i++) { digitalWrite(LED_PIN, HIGH); delay(150); digitalWrite(LED_PIN, LOW); delay(150); } delay(500); // reset with carry forward totalAmount = overflow; milestone25 = false; milestone50 = false; milestone75 = false; Serial.print("System Reset → Carry Forward: "); Serial.println(totalAmount); return; } checkMilestones(percentage); } // -------- LED -------- void blinkLED(int times) { for (int i = 0; i < times; i++) { digitalWrite(LED_PIN, HIGH); delay(120); digitalWrite(LED_PIN, LOW); delay(120); } } // -------- MILESTONES -------- void checkMilestones(int percentage) { if (!milestone25 && percentage >= 25) { milestone25 = true; Serial.println("Milestone: 25% reached"); } if (!milestone50 && percentage >= 50) { milestone50 = true; Serial.println("Milestone: 50% reached"); } if (!milestone75 && percentage >= 75) { milestone75 = true; Serial.println("Milestone: 75% reached"); } }